<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>lqe</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : 23/03/2004</div>
    <p>
      <b>lqe</b> -  linear quadratic estimator (Kalman Filter)  </p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>[K,X]=lqe(P21)   </tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>P21</b>
        </tt>: <tt>
          <b>syslin</b>
        </tt> list</li>
      <li>
        <tt>
          <b>K, X</b>
        </tt>: real matrices</li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
      <tt>
        <b>lqe</b>
      </tt> returns the Kalman gain for the filtering
    problem in continuous or discrete time.</p>
    <p>
      <tt>
        <b>P21</b>
      </tt> is a <tt>
        <b>syslin</b>
      </tt> list representing the system <tt>
        <b>P21=[A,B1,C2,D21] P21=syslin('c',A,B1,C2,D21) or P21=syslin('d',A,B1,C2,D21)</b>
      </tt>
    </p>
    <p>
    The input to <tt>
        <b>P21</b>
      </tt> is a white noise with variance:</p>
    <pre>

     [B1 ]               [Q  S]
BigV=[   ] [ B1' D21'] = [    ]
     [D21]               [S' R]
   
    </pre>
    <p>
      <tt>
        <b>X</b>
      </tt> is the solution of the stabilizing Riccati 
    equation and <tt>
        <b>A+K*C2</b>
      </tt> is stable.</p>
    <p>
    In continuous time:</p>
    <pre>

(A-S*inv(R)*C2)*X+X*(A-S*inv(R)*C2)'-X*C2'*inv(R)*C2*X+Q-S*inv(R)*S'=0
   
    </pre>
    <pre>

K=-(X*C2'+S)*inv(R)
   
    </pre>
    <p>
    In discrete time:</p>
    <pre>

X=A*X*A'-(A*X*C2'+B1*D21')*pinv(C2*X*C2'+D21*D21')*(C2*X*A'+D21*B1')+B1*B1'
   
    </pre>
    <p>
      <tt>
        <b>K=-(A*X*C2'+B1*D21')*pinv(C2*X*C2'+D21*D21')</b>
      </tt>
    </p>
    <p>
      <tt>
        <b>xhat(t+1)= E(x(t+1)| y(0),...,y(t))</b>
      </tt> (one-step predicted <tt>
        <b>x</b>
      </tt>)
    satisfies the recursion:</p>
    <pre>

xhat(t+1)=(A+K*C2)*xhat(t) - K*y(t).
   
    </pre>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>

//Assume the equations
//.
//x = Ax + Ge
//y = Cx + v
//with
//E ee' = Q_e,    Evv' = R,    Eev' = N 
//
//This is equivalent to
//.
//x = Ax  + B1 w
//y = C2x + D21 w
//with E { [Ge ]  [Ge v]' } = E { [B1w ] [B1w D21w]' } = bigR =
//         [ v ]                  [D21w]    
//
//[B1*B1'  B1*D21';
// D21*B1'  D21*D21']  
//=
//[G*Q_e*G' G*N;
// N*G' R]

//To find (B1,D21) given (G,Q_e,R,N) form bigR =[G*Q_e*G' G*N;N'*G' R].
//Then [W,Wt]=fullrf(bigR);  B1=W(1:size(G,1),:);
//D21=W(($+1-size(C2,1)):$,:)
//
//P21=syslin('c',A,B1,C2,D21);
//[K,X]=lqe(P21);

//Example:
nx=5;ne=2;ny=3;
A=-diag(1:nx);G=ones(nx,ne);
C=ones(ny,nx); Q_e(ne,ne)=1; R=diag(1:ny); N=zeros(ne,ny);
bigR =[G*Q_e*G' G*N;N'*G' R];
[W,Wt]=fullrf(bigR);B1=W(1:size(G,1),:);
D21=W(($+1-size(C,1)):$,:);
C2=C;
P21=syslin('c',A,B1,C2,D21);
[K,X]=lqe(P21);
//Riccati check:
S=G*N;Q=B1*B1';
(A-S*inv(R)*C2)*X+X*(A-S*inv(R)*C2)'-X*C2'*inv(R)*C2*X+Q-S*inv(R)*S'

//Stability check:
spec(A+K*C)
 
  </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="lqr.htm">
        <tt>
          <b>lqr</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="observer.htm">
        <tt>
          <b>observer</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
    <h3>
      <font color="blue">Author</font>
    </h3>
    <p>F. D.   </p>
  </body>
</html>
